﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CloudFox.Weave;
using System.Data.SqlClient;
using System.Configuration;

namespace BookmarkBrowser
{
    public static class BookmarkBrowserCommon
    {
        public static WeaveProxy BuildClient(string userName, string password, string syncKey)
        {
            string tag = "";

            try
            {
                var comm = new HttpCommunicationChannel();
                return new WeaveProxy(comm, userName, password, syncKey);
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    tag = ex.InnerException.ToString();
                }

                WriteEvent(ex.Message, DateTime.Now, ex.ToString(), ex.Source, "BuildClient", tag);
                throw;
            }
        }

        public static CloudFox.Presentation.Directory LoadBookmarks(WeaveProxy client)
        {
            string tag = "";

            try
            {
                var lstRoot = client.DecryptPayload<Bookmark>(client.GetCollection("bookmarks", null, null, null, -1, -1, CloudFox.Weave.SortOrder.Index));
                CloudFox.Presentation.Directory mainDir = CloudFox.Presentation.BookmarksStructureBuilder.Build(lstRoot);
                int count = lstRoot.Where(x => x.BookmarkType == BookmarkType.Bookmark).Count();
                mainDir.Tag = count.ToString();

                return mainDir;
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    tag = ex.InnerException.ToString();
                }

                WriteEvent(ex.Message, DateTime.Now, ex.ToString(), ex.Source, "LoadBookmarks", tag);
                throw;
            }
        }

        public static void WriteEvent(string desc, DateTime timestamp, string longDesc = "", string source = "", 
                                      string process = "", string tag = "")
        {
            SqlDataAccess.SqlDataAccess db;
            SqlConnectionStringBuilder connStrBuilder;
            string sql = null;
            string msg = null;

            try
            {
                connStrBuilder = new SqlConnectionStringBuilder();
                connStrBuilder.DataSource = ConfigurationManager.AppSettings["Server"];
                connStrBuilder.InitialCatalog = ConfigurationManager.AppSettings["Database"];
                connStrBuilder.UserID = ConfigurationManager.AppSettings["DBUserName"];
                connStrBuilder.Password = ConfigurationManager.AppSettings["DBPassword"];
                db = new SqlDataAccess.SqlDataAccess(connStrBuilder.ConnectionString);

                sql = "Insert Into Events ([Description], Timestamp, LongDescription, Source, ";
                sql = sql + "Process, Tag) Values (@Desc, @Timestamp, @LongDescription, ";
                sql = sql + "@Source, @Process, @Tag)";

                if (desc.Length <= 1000)
                {
                    db.AddSQLParmeter("@Desc", desc);
                }
                else
                {
                    db.AddSQLParmeter("@Desc", desc.Substring(0, 1000));
                }

                db.AddSQLParmeter("@Timestamp", timestamp);

                if (longDesc.Length <= 4000)
                {
                    db.AddSQLParmeter("@LongDescription", longDesc);
                }
                else
                {
                    db.AddSQLParmeter("@LongDescription", longDesc.Substring(0, 4000));
                }

                if (source.Length <= 50)
                {
                    db.AddSQLParmeter("@Source", source);
                }
                else
                {
                    db.AddSQLParmeter("@Source", source.Substring(0, 50));
                }

                if (process.Length <= 50)
                {
                    db.AddSQLParmeter("@Process", process);
                }
                else
                {
                    db.AddSQLParmeter("@Process", process.Substring(0, 50));
                }

                if (tag.Length <= 4000)
                {
                    db.AddSQLParmeter("@Tag", tag);
                }
                else
                {
                    db.AddSQLParmeter("@Tag", tag.Substring(0, 4000));
                }

                db.ExecuteNonQuery(sql);
            }
            catch (Exception ex)
            {
                msg = ex.Message.Trim();

                if (!string.IsNullOrEmpty(desc))
                {
                    msg = msg + Environment.NewLine + "Event description: " + desc;
                }

                msg = msg + Environment.NewLine + "Event timestamp: " + timestamp.ToString("M/dd/yyyy H:mm yy");

                if (!string.IsNullOrEmpty(longDesc))
                {
                    msg = msg + Environment.NewLine + "Event long description: " + longDesc;
                }
                if (!string.IsNullOrEmpty(source))
                {
                    msg = msg + Environment.NewLine + "Source: " + source;
                }
                if (!string.IsNullOrEmpty(process))
                {
                    msg = msg + Environment.NewLine + "Process: " + process;
                }
                if (!string.IsNullOrEmpty(tag))
                {
                    msg = msg + Environment.NewLine + "Tag: " + tag;
                }

                //WriteOSEventLog("Application", msg, "", EventLogEntryType.Error);
            }
        }


    }
}
